Equip={}
EquipFunc={}
EquipFunc.__index = EquipFunc
setmetatable(Equip, EquipFunc)

Equip.EquipType = {armour="军盔", uniform="军装", boots="军靴", special="特殊装备", cloak="披风", weapon="普通武器"}
Equip.EquipAttrDesc = {normalAttack="普通攻击力", tacticsAttack="战术攻击力", blastAttack="爆破攻击力", normalDefense="普通防御力", 
                  tacticsDefense="战术防御力", blastDefense="爆破防御力", forces="兵力", hit="增加命中",
                  dodge="增加闪避", crit="增加暴击几率", reduceNormalDefense="降低对方普通防御力", decorationXp="增加勋章经验1200",
                  morale="增加士气", antiBlock="增加反格挡", antiCrit="增加反暴击", critAttack="增加暴击伤害"}

Equip.QualityConfig = {
                        white={title="白色",id=1,rate=1,strengthenRate=1},
                        green={title="绿色",id=2,rate=1.2,strengthenRate=1.25},
                        blue={title="蓝色",id=3,rate=1.5,strengthenRate=1.5},
                        purple={title="紫色",id=4,rate=1.8,strengthenRate=1.75},
                        gold={title="金色",id=5,rate=2.1,strengthenRate=2}
                      }
Equip.EquipColor = {gray=ccc3(100,100,100), white=ccc3(255, 255, 255), green=ccc3(0,255,0), blue=ccc3(0, 0, 255), purple=ccc3(200, 100, 200), gold=ccc3(255, 153, 99)}
Equip.EquipColorById = {[1]=ccc3(255, 255, 255), [2]=ccc3(0,255,0), [3]=ccc3(0, 0, 255), [4]=ccc3(200, 100, 200), [5]=ccc3(255, 153, 99)}

EquipFunc.create = function(self, equipIndex)
  --equipIndex = 44
  local eq={}
  setmetatable(eq, EquipFunc)
  eq.equipInfo = UserData.userinfo.host.equips[tostring(equipIndex)]
  eq.equipConfig = UserData.config.models.Equip[eq.equipInfo.e] 
  print("EquipFunc.create", equipIndex, eq.equipConfig.name) 
  eq.equipTpl = UserData.config.models.EquipTemplate[eq.equipConfig.tid]
  eq.equipLevel = UserData.config.models.EquipLevel[math.floor((eq.equipConfig.level+10)/10)]
  --print("EquipFunc.create", (eq.equipConfig.level+10)/10, eq.equipLevel) 
  eq.equipQuality = Equip.QualityConfig[eq.equipConfig.quality] 
  eq.equipStrenthenLevel = UserData.config.models.EquipStrongLevel[eq.equipInfo.l]
  eq:getAttr1()
  eq:getAttr2()
  return eq
end

EquipFunc.getAttr1 = function(self)
  local attr = math.floor(self.equipTpl.attr1Value*self.equipLevel.rate*self.equipQuality.rate) + 
              math.floor(self.equipTpl.attr1UpValue*self.equipInfo.l*self.equipQuality.strengthenRate * self.equipLevel.strengthenRate);
  local desc = Equip.EquipAttrDesc[self.equipTpl.attr1]
  print(desc, attr)
  return self.equipTpl.attr1,desc,attr
end

EquipFunc.getAttr2 = function(self)
  local attr = math.floor(self.equipTpl.attr2Value*self.equipLevel.rate*self.equipQuality.rate) + 
              math.floor(self.equipTpl.attr2UpValue*self.equipInfo.l*self.equipQuality.strengthenRate * self.equipLevel.strengthenRate);
  local desc = Equip.EquipAttrDesc[self.equipTpl.attr2]
  print(desc, attr)
  return self.equipTpl.attr2,desc,attr
end

EquipFunc.getStrenthenSuccessRate = function(self)
  local nextStrenthenLevel = UserData.config.models.EquipStrongLevel[self.equipStrenthenLevel.nextLevel]
  return nextStrenthenLevel.successRate
end

EquipFunc.getSellCash = function(self)
--return equip.baseSellCash * equipLevel.id * 2 / 3 * equipQuality.rate * (1 + equipStrengthenLevelId / 80);
  return self.equipTpl.baseSellCash*self.equipLevel.id*2/3*self.equipQuality.rate*(1+self.equipInfo.l/80)
end

EquipFunc.getStrenthenCash = function(self)
  --return (model.equip.baseStrengthenCash + model.equip.baseStrengthenCash * (model.nextStrengthenLevel.id * model.nextStrengthenLevel.id / 50)) * EquipQualityEnum.getByName(model.equip.quality).rate * model.equipLevel.rate;
  
  local nextStrenthenLevel = UserData.config.models.EquipStrongLevel[self.equipStrenthenLevel.nextLevel]
  local cash = (self.equipTpl.baseCost + self.equipTpl.baseCost*nextStrenthenLevel.id*nextStrenthenLevel.id/50)*self.equipQuality.rate*self.equipLevel.rate
  print("EquipFunc.getStrenthenCash", cash)
  return cash
end

EquipFunc.createTip = function(self, target)
  local tipLayer = CCBReader:load("EquipProperty.ccbi")
  local spBK = tipLayer:getChildByTag(1001)
  local sp = CCSprite:create(nameFromPath(self.equipConfig.asset))
  sp:setPosition(spBK:getPosition())
  tipLayer:addChild(sp)
  local lb = tipLayer:getChildByTag(1002)
  TypeCast(lb, "CCLabelTTF"):setString(self.equipConfig.name)
  lb:setColor(Equip.EquipColor[self.equipConfig.quality])
  lb = tipLayer:getChildByTag(1010)
  if target.selectedType==0 then
    TypeCast(lb, "CCLabelTTF"):setString("装备")
  else
    TypeCast(lb, "CCLabelTTF"):setString("卸下")
  end
  setObjectExtTable(tipLayer:getChildByTag(2001), target)
  setObjectExtTable(tipLayer:getChildByTag(2002), target)
  setObjectExtTable(tipLayer:getChildByTag(2003), target)


  local lb = CCLabelTTF:create(string.format("强化等级：%d级", self.equipInfo.l), "Helvetica", 28)  
  lb:setPosition(ccp(440,400))
  lb:setAnchorPoint(ccp(0,0.5))
  tipLayer:addChild(lb)


  local height = 340
  lb = CCLabelTTF:create(string.format("佩戴条件：将领%d级", self.equipConfig.level), "Helvetica", 28)  
  lb:setPosition(ccp(350,height))
  lb:setAnchorPoint(ccp(0,0.5))
  tipLayer:addChild(lb)
  height = height-40

  local sellcash = self:getSellCash()  
  lb = CCLabelTTF:create(string.format("出售价格：%d现金", sellcash), "Helvetica", 28)  
  lb:setPosition(ccp(350,height))
  lb:setAnchorPoint(ccp(0,0.5))
  tipLayer:addChild(lb)
  height = height-40
  

  local key, desc, attr = self:getAttr1() 
  local lb = CCLabelTTF:create(string.format("%s：+%d", desc, attr), "Helvetica", 28)  
  lb:setPosition(ccp(350,height))
  lb:setAnchorPoint(ccp(0,0.5))
  tipLayer:addChild(lb)
  lb:setColor(ccc3(255, 150, 0))
  height = height-40
 
  key, desc, attr = self:getAttr2() 
  if desc~=nil then
    local lb = CCLabelTTF:create(string.format("%s：+%d", desc, attr), "Helvetica", 28)  
    lb:setPosition(ccp(350,height))
    lb:setAnchorPoint(ccp(0,0.5))
    tipLayer:addChild(lb)
    lb:setColor(ccc3(255, 150, 0))
    height = height-40
  end
  return tipLayer 
  --[[
  local lb = CCLabelTTF:create(string.format("要求：需要将领等级%d", self.equipConfig.level), "Helvetica", 28)  
  lb:setPosition(ccp(350,260))
  tipLayer:addChild(lb)

  local lb = CCLabelTTF:create(string.format("要求：需要将领等级%d", self.equipConfig.level), "Helvetica", 28)  
  lb:setPosition(ccp(350,220))
  tipLayer:addChild(lb)
  ]]
end
